/**
 *  Created on  : 14/04/2012
 *  Author      : Ing. Javier Silva Pérez - [javier]
 *  Description :
 *  	
 */
package cinvestav.cryptography.pki.android.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.cert.CertificateException;

import cinvestav.cryptography.pki.android.exception.AndroidCryptoUtilsException;

/**
 * @author Ing. Javier Silva Pérez - [javier]
 * 
 */
public class AndroidKeyStoreUtil implements IAndroidKeyStoreUtils {

	/**
	 * 
	 */
	public AndroidKeyStoreUtil() {
		// TODO Auto-generated constructor stub
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cinvestav.cryptography.android.utils.IAndroidKeyStoreUtils#createNewKeyStore
	 * ()
	 */
	@Override
	public KeyStore createNewKeyStore() throws AndroidCryptoUtilsException {
		// TODO Auto-generated method stub
		KeyStore ks;
		try {
			ks = KeyStore.getInstance("BKS", AndroidCryptoUtils.PROVIDER);
			ks.load(null, null);
			return ks;
		} catch (KeyStoreException e) {
			// TODO Auto-generated catch block
			throw new AndroidCryptoUtilsException(
					"Error creating the KeyStore: " + e.getMessage(),
					e.getCause());
		} catch (NoSuchProviderException e) {
			// TODO Auto-generated catch block
			throw new AndroidCryptoUtilsException(
					"Error creating the KeyStore: " + e.getMessage(),
					e.getCause());
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			throw new AndroidCryptoUtilsException(
					"Error creating the KeyStore: " + e.getMessage(),
					e.getCause());
		} catch (CertificateException e) {
			// TODO Auto-generated catch block
			throw new AndroidCryptoUtilsException(
					"Error creating the KeyStore: " + e.getMessage(),
					e.getCause());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			throw new AndroidCryptoUtilsException(
					"Error creating the KeyStore: " + e.getMessage(),
					e.getCause());
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cinvestav.cryptography.android.utils.IAndroidKeyStoreUtils#saveKeyStore
	 * (java.security.KeyStore, java.lang.String, java.lang.String)
	 */
	@Override
	public void saveKeyStore(KeyStore ks, String keyStoreFullPath,
			String password) throws AndroidCryptoUtilsException {
		// TODO Auto-generated method stub
		File file = new File(keyStoreFullPath);
		FileOutputStream fos;
		try {
			fos = new FileOutputStream(file);
			ks.store(fos, password.toCharArray());
			fos.flush();
			fos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			throw new AndroidCryptoUtilsException(
					"Error storing the KeyStore: " + e.getMessage(),
					e.getCause());
		} catch (KeyStoreException e) {
			// TODO Auto-generated catch block
			throw new AndroidCryptoUtilsException(
					"Error storing the KeyStore: " + e.getMessage(),
					e.getCause());
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			throw new AndroidCryptoUtilsException(
					"Error storing the KeyStore: " + e.getMessage(),
					e.getCause());
		} catch (CertificateException e) {
			throw new AndroidCryptoUtilsException(
					"Error storing the KeyStore: " + e.getMessage(),
					e.getCause());
		} catch (IOException e) {
			throw new AndroidCryptoUtilsException(
					"Error storing the KeyStore: " + e.getMessage(),
					e.getCause());
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cinvestav.cryptography.android.utils.IAndroidKeyStoreUtils#loadKeyStore
	 * (java.lang.String, java.lang.String)
	 */
	@Override
	public KeyStore loadKeyStore(String keyStoreFullPath,
			String keyStorePassword) throws AndroidCryptoUtilsException {
		KeyStore ks;
		File keyStoreFile = new File(keyStoreFullPath);
		FileInputStream fis = null;
		try {

			fis = new FileInputStream(keyStoreFile);
		} catch (FileNotFoundException ex) {
			throw new AndroidCryptoUtilsException(
					"KeyStoreFile doesn´t exist: " + ex.getMessage(),
					ex.getCause());
		}
		try {
			ks = KeyStore.getInstance("BKS", AndroidCryptoUtils.PROVIDER);
			ks.load(fis, keyStorePassword.toCharArray());
			return ks;
		} catch (IOException ex) {
			throw new AndroidCryptoUtilsException(
					"Error loading the KeyStore File: Wrong file format\n"
							+ ex.getMessage(), ex.getCause());
		} catch (NoSuchAlgorithmException ex) {
			throw new AndroidCryptoUtilsException(
					"Error loading the KeyStore File: the algorithm used to check the integrity of the keystore cannot be found\n"
							+ ex.getMessage(), ex.getCause());
		} catch (CertificateException e) {
			// TODO Auto-generated catch block
			throw new AndroidCryptoUtilsException(
					"Error loading the KeyStore File: any of the certificates in the keystore could not be loaded\n"
							+ e.getMessage(), e.getCause());
		} catch (KeyStoreException e) {
			// TODO Auto-generated catch block
			throw new AndroidCryptoUtilsException(
					"Error loading the KeyStore File: " + e.getMessage(),
					e.getCause());
		} catch (NoSuchProviderException e) {
			// TODO Auto-generated catch block
			throw new AndroidCryptoUtilsException(
					"Error loading the KeyStore File: " + e.getMessage(),
					e.getCause());
		}
	}

}
